package com.fsxgt.datagrid.console.controller;

import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson2.JSONObject;
import com.fsxgt.datagrid.console.exception.RunningException;
import com.fsxgt.datagrid.console.service.impl.ExecutorService;
import com.fsxgt.datagrid.console.service.impl.ServerService;
import com.fsxgt.datagrid.core.exception.DataGridException;
import com.fsxgt.datagrid.core.utils.ReturnT;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(tags = "执行接口", value = "ExecutorController")
@RequestMapping(produces = "application/json; charset=UTF-8")
@RestController
public class ExecutorController extends BaseController {


	@Autowired
    private ServerService serverService;
	@Autowired
    private ExecutorService executorService;
	


	@ApiOperation(hidden = true, value = "调用executor", notes = "")
	@RequestMapping(path = "/executor/start", method = RequestMethod.POST)
	public Object start(@RequestBody JSONObject param, HttpServletRequest request) throws RunningException {
		

		
		String server=serverService.getServer();
		
		log.info("executor:"+server);
		
		String execId = UUID.randomUUID().toString();
		
		String objId=param.getString("objId");
		String objType=param.getString("objType");
		
		Map context=param.getObject("context", Map.class);


		return executorService.start(objId,objType, server,execId,new Date(),context);
		
	}
	
	
	@ApiOperation(hidden = true, value = "调用executor", notes = "")
	@RequestMapping(path = "/executor/stop", method = RequestMethod.POST)
	public Object stop(@RequestBody JSONObject param, HttpServletRequest request) throws DataGridException, IOException {
		
		String execId=param.getString("execId");
		String server=param.getString("server");

		return executorService.stop(execId,server);
		
	}

	
	@ApiOperation(hidden = true, value = "调用executor", notes = "")
	@RequestMapping(path = "/executor/getLog", method = RequestMethod.POST)
	public Object getLog(@RequestBody JSONObject param, HttpServletRequest request) throws DataGridException, IOException {
		

		String server=param.getString("server");
		String triggerTime=param.getString("triggerTime");
		String execId=param.getString("execId");
		
		return executorService.getLog(execId, server,triggerTime);
		
	}


	@ApiOperation(hidden = true, value = "调用executor", notes = "")
	@RequestMapping(path = "/executor/callback", method = RequestMethod.POST)
	public Object callback(@RequestBody JSONObject param, HttpServletRequest request) throws Exception {
		
		String overTime=param.getString("overTime");
		String startTime=param.getString("startTime");
		String status=param.getString("status");
		String msg=param.getString("msg");
		String logPath=param.getString("logPath");
		String execId=param.getString("execId");
		String objId=param.getString("objId");
		String objType=param.getString("objType");

		log.info("callback:"+param.toJSONString());
		executorService.callback(overTime, startTime, status, msg, logPath, execId, objId,objType);
		
		return new ReturnT();
		
	}

}